home *** CD-ROM | disk | FTP | other *** search
- Path: newshost.lanl.gov!tanmoy
- From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
- Newsgroups: comp.lang.c
- Subject: Re: Initialising structure members - help please!
- Date: 20 Feb 1996 16:45:14 GMT
- Organization: Los Alamos National Laboratory
- Message-ID: <TANMOY.96Feb20094514@qcd.lanl.gov>
- References: <4gb8hn$3m8@news.mistral.co.uk> <31298EF1.5F2C@cmt.lpr.mail.carel.fi>
- <4gckb1$77e@news.mistral.co.uk>
- NNTP-Posting-Host: qcd.lanl.gov
- Mime-Version: 1.0
- Content-Type: text
- In-reply-to: mikebarnard@mistral.co.uk's message of Tue, 20 Feb 1996 11:54:07 GMT
-
- In article <4gckb1$77e@news.mistral.co.uk> mikebarnard@mistral.co.uk
- (Mike Barnard) writes:
- <snip>
- >You can't copy C strings with an "=". You must use the strcpy() function.
-
- and...
-
- >Since you declared 'description' as an array of chars, use strcpy(one.description,
- >"whatever string"); Be sure you won't use longer strings than the space available. A
- >better approach would be to declare 'decription' as a 'char *' and then malloc
-
- Yet better is to declare it const char * then. String literals are
- arrays of type `char []', which in most uses decay to `char*' and can
- be assigned to `const char*'. However, like `const char[]' objects,
- string literals cannot be modified portably. Unlike `const char[]' objects,
- attempts to modify them do not, by themselves, require a diagnostic
- from the compiler.
-
- Assigning them to `const char*' guarantees that unless explicit casts
- (or other tricks involving library functions) are used, any attempt to
- modify them will generate a diagnostic.
-
- >enough space for whatever string you're going to place into it. (Also, your syntax
- >would work in that case.) The last two will work if you add & in front of them.
-
- Thanks, I'll look at what this means. But as a learning question, (not
- an argumentative one!!!), why does this good old boy work then?
-
- char name[]="An array of characters";
-
- Surely "name" is an array of characters as is my "description"? It
- works here. Doesn't it? This leads me on to another question actually,
- but I'll start another thread with it I think.
-
- Normally, in C, arrays cannot be assigned from other arrays. The
- reason for this is historical: arrays are not really first class
- citizens of the C type system. Whenever one uses an array from memory
- in an expression, _except_ as an immediate argument of & (address of)
- or sizeof operators, it is converted to a pointer to its first element
- instead. This conversion is usually referred to as the `decay of the
- array'. In other words, in almost all expressions, an array a can be
- replaced by the expression (&a[0]).
-
- Now, here is the problem. A variable declared as an array is an
- lvalue: i.e. it refers to a region of storage. However, (&a[0]) is an
- expression which designates a pointer: it does not designate any
- region of memory. What would it mean to change it? As (&a[0]) =
- (&b[0]) does not make sense, neither does a = b! Note that even if we
- had changed the rules of C to say that lhs of = does not decay, we
- still would have a problem of trying to assign a pointer to an
- array. We could have had a rule which says that neither side of the =
- decays, but that would involve copying entire arrays; and in the early
- days of C, people thought that compilers should not be so `smart' and
- do complicated things `behind programmer's back'. Now, it is too late
- to change this.
-
- Now, declarations are different. An array being declared is not
- sitting inside an expression (naturally). The rule for decays applies
- only for arrays in expressions. Remembering that the initializer (the
- stuff that initializes a declared object: a part of the declaration,
- not a separate statement), is in an expression context; the language
- still does not allow `int a[] = b' where b is an array (how can one
- initialize an array with a pointer to which b decays?). In fact, for
- non-simple objects (i.e. things like arrays and structs), the compiler
- says that whatever you use to initialize it must be a bunch of
- constants. So, typically, one writes `int a[] = {1,2,3}' to initialize
- an array.
-
- At this point, one notices that one kind of initialization is very
- common: a character array. This could always be written in the usual
- form: `char a[] = {'h', 'e', 'l', 'l', 'o', '\0'}', but people decided
- that `char a[] = "hello"' would be a useful shorthand for this. Note
- that string literals (and wide string literals) are special: they are
- the only constants which are arrays, and a little leeway for them
- doesn't hurt :-)
-
- Note that there is another peculiarity. "hello" normally expands to
- {'h', 'e', 'l', 'l', 'o', '\0'} as mentioned above, but when
- initializing an array whose length is specified as 5, it is equivalent
- to {'h', 'e', 'l', 'l', 'o'} This special treatment of the ending '\0'
- is also part of the magic of string literal initializers.
-
- And by the way, though the string literal arrays are constant, they
- can happily initialize non-constant arrays. In fact, in many respects,
- it is best to think of them not as literals at all in this context,
- but as an abbreviation mentioned in the last paragraph.
-
- Do things clear up somewhat now?
-
- Cheers
- Tanmoy
- --
- tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
- Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
- Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
- <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
- internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
- fax: 1 (505) 665 3003 voice: 1 (505) 665 4733 [ Home: 1 (505) 662 5596 ]
-